const command = [
  'mysave',
  'exporttoword',
  'project',
  'insertCatalog',
  'slideshow',
  'preview',
  'clearlink',
  'clearAllContent',
];

CKEDITOR.plugins.add("mycustomplugin", {
  // requires: ["dialog"],
  init: function (editor) {
    for (let commandName of command) {
      editor.addCommand(commandName, {
        exec: (editor, a, b, c) => {
          console.log(commandName);
        }
      });
    }

    editor.ui.addButton("mySave", {
      label: "保存",//title
      command: "mysave",//命令
      icon: this.path + "icons/mySave.png"//在toolbar中的图标
    });
    editor.ui.addButton("ExportToWord", {
      label: "导出为Word",
      command: "exporttoword",
      icon: this.path + "icons/ExportToWord.png"
    });
    editor.ui.addButton("clearAllContent", {
      label: "清除所有内容",
      command: "clearAllContent",
      icon: this.path + "icons/clearAll.png"
    });

    editor.addMenuItems && editor.addMenuItems({
      insertCatalog: {
        label: "插入示证目录",
        command: 'insertCatalog',
        group: 'mycustom',
        icon: this.path + "icons/ExportToWord.png"
        // group: 'image',
        // icon: '',
        // order: 1
      },
      preview: {
        label: "预览",
        command: 'preview',
        group: 'mycustom',
        icon: this.path + "icons/ExportToWord.png"
        // group: 'image',
        // icon: '',
        // order: 1
      },
      project: {
        label: "投影",
        command: 'project',
        group: 'mycustom',
        icon: this.path + "icons/ExportToWord.png"
        // group: 'image',
        // icon: '',
        // order: 1
      },
      slideshow: {
        label: "播放幻灯片",
        command: 'slideshow',
        group: 'mycustom',
        icon: this.path + "icons/ExportToWord.png"
        // group: 'image',
        // icon: '',
        // order: 1
      },
      clearlink: {
        label: "清除超链接",
        command: 'clearlink',
        group: 'mycustom',
        icon: this.path + "icons/clear.png"
        // group: 'image',
        // icon: '',
        // order: 1
      },
    });
    editor.contextMenu && editor.contextMenu.addListener((element, selection) => {

      let el = element.$;
      // let isPreview = false;
      // if (element.getName() == 'a') {
      //   let href = el.href;
      //   let hrefRE = /jzbh:[\s\S]+;MLBH:[\s\S]+;WJXH:[\s\S]+DossierHyperLink:[\s\S]+/i;
      //   isPreview = hrefRE.test(href)
      // }
      // `jzbh:51200000001023;MLBH:69C7120F4C244CAEAF2077D09F3B256F;WJXH:053012FF-EA3C-4669-B3D9-7E9CFB3368B7%7b017473FE-0508-4EB6-8408-3D3210C226ED%7dDossierHyperLink:\\%7b9DC6F047-260A-400B-BC28-94FB826C4343%7d`

      // console.log(element.getName());
      // console.log(selection.getCommonAncestor());
      // console.log(selection.getStartElement());
      let isLink = el.className === 'MsoHyperlink';
      if (isLink) {
        // console.log(window.getSelection(), el)
        // window.getSelection().selectAllChildren(el)
        // selectAllChildren
        return {
          // insertCatalog: CKEDITOR.TRISTATE_OFF,
          // preview: CKEDITOR.TRISTATE_OFF,
          // slideshow: CKEDITOR.TRISTATE_OFF,
          // project: CKEDITOR.TRISTATE_OFF,
          clearlink: CKEDITOR.TRISTATE_OFF,
          // cut: CKEDITOR.TRISTATE_DISABLED,
          // copy: CKEDITOR.TRISTATE_DISABLED,
        };
      } else {
        return {}
      }
    });

  }
})
